package api

import (
	"api-gatway/basic/config"
	"api-gatway/handler/request"
	"api-gatway/pkg"
	"fmt"

	__ "api-gatway/basic/proto"
	"github.com/gin-gonic/gin"

	"net/http"
	"path/filepath"
)

func Upload(c *gin.Context) {
	file, _ := c.FormFile("file")
	ext := filepath.Ext(file.Filename)
	if ext != ".mp4" {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"mes":  "视频类型必须是mp4",
		})
		return
	}
	if file.Size > 500*1024*1024 {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"mes":  "视频大小不能超过500mb",
		})
		return
	}
	dsn := "D:\\gowork\\src\\dy\\api-gatway\\static\\upload\\" + file.Filename
	c.SaveUploadedFile(file, dsn)
	pkg.Oss(file.Filename, dsn)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "视频上传成功",
		"data": fmt.Sprintf("https://wangziyue123.oss-cn-shanghai.aliyuncs.com/") + file.Filename,
	})
}

func Login(c *gin.Context) {
	var param request.Login
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
		return
	}
	r, err := config.UserCline.LoginUser(c, &__.LoginUserReq{
		Phone: param.Phone,
		Code:  param.Code,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"mes":  "自动注册成功",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "登录成功",
		"data": r.UserId,
	})
}
